from django.contrib.auth.models import AbstractUser
from django.db import models


# Create your models here.
class User(AbstractUser):
    username = models.CharField(max_length=50, null=False, verbose_name="用户名", unique=True)
    name = models.CharField(max_length=50, null=True, verbose_name="名字")
    text = models.CharField(max_length=100, null=True, verbose_name="个性签名")
    vip = models.BooleanField(default=False, null=False, verbose_name="是否为VIP")
    avatar = models.CharField(max_length=100, null=True, verbose_name="头像链接")
    b1 = models.IntegerField(default=0, null=False, verbose_name="点赞")
    b2 = models.IntegerField(default=0, null=False, verbose_name="关注")
    b3 = models.IntegerField(default=0, null=False, verbose_name="收藏")

    REQUIRED_FIELDS = ['name', 'email', 'text', 'vip']

    class Meta:
        db_table = "user"
        verbose_name = '用户表'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return self.name


class Upload(models.Model):
    uploader = models.ForeignKey(User, related_name="images", on_delete=models.CASCADE,
                                 verbose_name="上传者", null=False)
    url = models.CharField(max_length=200, default=None, null=True, verbose_name="超链接")
    time = models.DateTimeField(auto_now_add=True, verbose_name="上传时间")

    text = models.CharField(max_length=400, default=None, null=True, verbose_name="密文")

    class Meta:
        db_table = "upload"
        verbose_name = '上传文件表'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return self.uploader.name + " " + str(self.time)


class Praise(models.Model):
    image = models.ForeignKey(Upload, related_name="image_praises", on_delete=models.CASCADE,
                              verbose_name="图片", null=False)
    user = models.ForeignKey(User, related_name="user_praise", on_delete=models.CASCADE,
                             verbose_name="上传者", null=False)
    time = models.DateTimeField(auto_now_add=True, verbose_name="点赞时间")

    class Meta:
        db_table = "praise"
        verbose_name = '点赞表'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return f"{self.image_id} {self.user_id}"


class Comment(models.Model):
    image = models.ForeignKey(Upload, related_name="image_comments", on_delete=models.CASCADE,
                              verbose_name="图片", null=False)
    user = models.ForeignKey(User, related_name="user_comments", on_delete=models.CASCADE,
                             verbose_name="上传者", null=False)
    time = models.DateTimeField(auto_now_add=True, verbose_name="评论时间")
    text = models.CharField(max_length=300, default="", verbose_name="评论", null=True)

    class Meta:
        db_table = "comment"
        verbose_name = '评论表'
        verbose_name_plural = verbose_name
        ordering = ('id',)
